package com.ymatou.iapi.synctrade.parameter.mongo;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;

public class SourceData {

	private Map<String, Object> orderDetail;
	private List<Map<String, Object>> products;
	private List<Map<String, Object>> refunds;
	private List<Map<String, Object>> subProducts;
	private List<Map<String, Object>> statusLogs;
	private List<Map<String, Object>> thirdpartyRefundStatus;
	private Map<String, Object> delivery;
	private List<String> bizType;
	private List<String> deliveryCodes;
	private List<String> prepaidCardNo;
	private List<Map<String, Object>> prepaidCardRefund;
	private List<Map<String, Object>> productProxies;
	private List<Map<String, Object>> shenqiSellerProfits;
	private List<Map<String, Object>> subCodes;
	private List<Map<String, Object>> pickup;

	public SourceData() {
	}

	public SourceData(List<Map<String, Object>> o) {
		buildOrder(o);
	}

	public SourceData buildOrder(List<Map<String, Object>> o) {
		return buildOrder(o, null, null, null, null);
	}

	public SourceData buildOrder(List<Map<String, Object>> o,
			List<Map<String, Object>> prd) {
		return buildOrder(o, null, prd, null, null);
	}

	public SourceData buildOrder(List<Map<String, Object>> o,
			List<Map<String, Object>> oExt, List<Map<String, Object>> pExt) {
		return buildOrder(o, null, null, oExt, pExt);
	}

	public SourceData buildOrder(List<Map<String, Object>> o,
			List<Map<String, Object>> op, List<Map<String, Object>> prd,
			List<Map<String, Object>> oExt, List<Map<String, Object>> pExt) {
		if (CollectionUtils.isEmpty(o) || o.size() > 1) {
			//throw new Exception();
			//return 
		}

		orderDetail = o.get(0);

		if (!CollectionUtils.isEmpty(op) && op.size() == 1 && op.get(0) != null) {
			orderDetail.putAll(op.get(0));
		}

		if (!CollectionUtils.isEmpty(prd)) {
			products = prd;
		}

		if (!CollectionUtils.isEmpty(oExt)) {
			if (oExt.size() != 1) {
				//throw new Exception();
			}
			orderDetail.putAll(oExt.get(0));
		}

		if (!CollectionUtils.isEmpty(pExt)) {
			if (CollectionUtils.isEmpty(products)) {
				products = pExt;
			} else {
				products.forEach(i -> {
					String itemId = "prds.itemId".substring(5);
					Map<String, Object> map = pExt.stream()
							.filter(j -> i.get(itemId).equals(j.get(itemId)))
							.findFirst().orElse(null);
					if (map != null) {
						i.putAll(map);
					}
				});
			}
		}
		return this;
	}
	public SourceData buildDelivery(List<String> dlvCodes) {
		return buildDelivery(null, null, dlvCodes);
	}

	public SourceData buildDelivery(List<Map<String, Object>> dlv,
			List<String> dlvTypes) {
		return buildDelivery(dlv, dlvTypes, null);
	}

	public SourceData buildDelivery(List<Map<String, Object>> dlv,
			List<String> dlvTypes, List<String> dlvCodes) {
		if (!CollectionUtils.isEmpty(dlv)) {
			if (dlv.size() != 1) {
				//throw new Exception();
			}
			delivery = dlv.get(0);
		}
		if (!CollectionUtils.isEmpty(dlvCodes)) {
			deliveryCodes = dlvCodes;
		}

		if (!CollectionUtils.isEmpty(dlvTypes)) {
			bizType = dlvTypes;
		}
		return this;
	}

	public SourceData buildRefund(List<Map<String, Object>> rfd) {
		if (!CollectionUtils.isEmpty(rfd)) {
			refunds = rfd;
		}
		return this;
	}

	public SourceData buildSubProducts(List<Map<String, Object>> subs) {
		if (!CollectionUtils.isEmpty(subs)) {
			subProducts = subs;
		}
		return this;
	}

	public SourceData buildStatusLog(List<Map<String, Object>> logs) {
		if (!CollectionUtils.isEmpty(logs)) {
			statusLogs = logs;
		}
		return this;
	}

	public SourceData buildPrepaidCard(List<String> ppcNo,
			List<Map<String, Object>> ppcRfd) {
		if (!CollectionUtils.isEmpty(ppcNo)) {
			prepaidCardNo = ppcNo;
		}
		if (!CollectionUtils.isEmpty(ppcRfd)) {
			prepaidCardRefund = ppcRfd;
		}
		return this;
	}
	
	public SourceData buildThirdpartyRefundStatus(List<Map<String, Object>> thirdRefSt) {
		
		if (!CollectionUtils.isEmpty(thirdRefSt)) {
			thirdpartyRefundStatus = thirdRefSt;
		}
		
		return this;
	}
	
	public SourceData buildProductProxies(List<Map<String, Object>> proxies) {
		
		if (!CollectionUtils.isEmpty(proxies)) {
			productProxies = proxies;
		}
		
		return this;
	}
	
	public SourceData buildShenqiSellerProfits(List<Map<String, Object>> profits) {
		if (!CollectionUtils.isEmpty(profits)) {
			this.shenqiSellerProfits = profits;
		}
		return this;
	}
	
	public SourceData buildOrderDetailSubCodes(List<Map<String, Object>> subCodes) {
		
		if (!CollectionUtils.isEmpty(subCodes)) {
			this.subCodes = subCodes;
		}
		
		return this;
	}
	
	public SourceData buildPickup(List<Map<String, Object>> pickup) {

		if (!CollectionUtils.isEmpty(pickup)) {
			this.pickup = pickup;
		}

		return this;
	}

	public List<String> getDeliveryCodes() {
		return deliveryCodes;
	}

	public Map<String, Object> getOrderDetail() {
		return orderDetail;
	}

	public Map<String, Object> getDelivery() {
		return delivery;
	}

	public List<String> getBizType() {
		return bizType;
	}

	public List<Map<String, Object>> getProducts() {
		return products;
	}

	public List<Map<String, Object>> getRefunds() {
		return refunds;
	}

	public List<Map<String, Object>> getSubProducts() {
		return subProducts;
	}

	public List<Map<String, Object>> getStatusLogs() {
		return statusLogs;
	}
	
	public List<Map<String, Object>> getThirdpartyRefundStatus() {
		return thirdpartyRefundStatus;
	}

	public List<String> getPrepaidCardNo() {
		return prepaidCardNo;
	}

	public List<Map<String, Object>> getPrepaidCardRefund() {
		return prepaidCardRefund;
	}

	public List<Map<String, Object>> getProductProxies() {
		return productProxies;
	}

	public List<Map<String, Object>> getShenqiSellerProfits() {
		return shenqiSellerProfits;
	}

	public List<Map<String, Object>> getSubCodes() {
		return subCodes;
	}

	public List<Map<String, Object>> getPickup() {
		return pickup;
	}
}
